//
//  OPPrivateMsg.m
//  OPFramework
//
//  Created by malcom on 7/24/08.
//  Copyright 2008 malcom-mac software. All rights reserved.
//

#import "OPPrivateMsg.h"
#import "OPBoard.h"

@implementation OPPrivateMsg

@synthesize p_msgID;
@synthesize p_author;
@synthesize p_sendDate;
@synthesize p_subject;
@synthesize p_text;

- (id) _initLoadingDataResponse:(NSDictionary *) _data forBoard:(OPBoard *) _board {
	self = [super init];
	if (self != nil) {
		_delegateBoard = [_board retain];
		
		// message id
		p_msgID = [[_data objectForKey: @"msg_id"] retain];
		
		// author
		p_author = [[_delegateBoard.obj_membersCache getUserMemberFromID: [NSNumber numberWithInt: [[_data objectForKey: @"user_id"] intValue]] 
					 withName: [_data objectForKey: @"username"]] retain];
		
		// send date
		p_sendDate = [[NSDate dateWithTimeIntervalSince1970: [[_data objectForKey:@"message_time"] longLongValue]] retain];
		
		// subject
		p_subject = [[_data objectForKey: @"message_subject"] retain];

		// text
		p_text = [[_data objectForKey: @"message_text"] retain];
		
		// folder id
		_folderID = [[_data objectForKey: @"folder_id"] retain];

	}
	return self;
}

- (void) dealloc {
	[p_msgID release];
	[p_author release];
	[p_sendDate release];
	[p_text release];
	[p_subject release];
	[_folderID release];
	[super dealloc];
}


- (NSString *) description {
	return [NSString stringWithFormat: @"OPPrivateMsg: %@ by %@ (%d bytes posted on %@)",p_subject,p_author.p_username,[p_text length],[p_sendDate description]];
}

- (BOOL) markAsRead {
	XMLRPCResponse *unreadMsgs = [_delegateBoard _executeRequestMethodNamed: @"user.setPrivateMessageAsRead"
																 withParams: [NSArray arrayWithObjects: p_msgID,_folderID,nil]
															 attachAuthData: NO];	
	if ([unreadMsgs isFault]) return NO;
	return YES;
}

#define _checkArray [NSArray arrayWithObjects: OPPrivateMsg_Options_EnableSign,OPPrivateMsg_Options_EnableSmiles,OPPrivateMsg_Options_EnableBBCode,OPPrivateMsg_Options_EnableURLs,OPPrivateMsg_Options_PutInOutbox,nil]

- (BOOL) replyWithMessage:(NSString *) _message withOptions:(NSDictionary *) _options {	
	return [OPPrivateMsg _sendMessageWithMode: @"reply" toUser:[NSNumber numberWithInt: p_author.p_userid] withSubject:p_subject text:_message replyTo:p_msgID options:_options board: _delegateBoard];
}

+ (BOOL) _sendMessageWithMode:(NSString *) _mode toUser:(NSNumber *) _userid withSubject:(NSString *) _subj text:(NSString *) _text replyTo:(NSNumber *) _msgid options:(NSDictionary *) _options board:(OPBoard *) _board {
	NSMutableArray *_params = [[NSMutableArray alloc] initWithObjects: _mode,_userid,_subj,_text,(_msgid == nil ? @"" : _msgid),nil];
	
	for (NSString *cKey in _checkArray) {
		NSNumber *num = [_options objectForKey: cKey];
		if (num == nil) num = [NSNumber numberWithBool: YES];
		[_params addObject: num];
	}
	
	XMLRPCResponse *sendMsg = [_board _executeRequestMethodNamed: @"user.sendPrivateMessage"
															  withParams: [_params autorelease]
														  attachAuthData: NO];	
	if ([sendMsg isFault]) return NO;
	return YES;
}


@end
